/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;
import beans.MemoriaForm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author RICARDO
 */
public class mostrarmemoriaAction extends Action{
        
    static Connection con  = null;
    static Statement  st   = null;  
    static ResultSet  res  = null; 
    static String     sql  = null;
    private static final String DBH = "jdbc:hsqldb:mem:memoriadb";
            
	public mostrarmemoriaAction() {
		// TODO Auto-generated constructor stub
	}
	@Override
        public ActionForward execute (ActionMapping mapping, 
				ActionForm form, 
				HttpServletRequest request, 
				HttpServletResponse response)throws Exception{
           
          //mostramos BD de memoria

            try
        {
            Class.forName("org.hsqldb.jdbcDriver"); //Load HSQLDB driver
            System.out.println("* Loading HSQLDB driver...");
        } 
        catch(Exception e)
        {  
            System.err.println("ERROR: failed to load HSQLDB JDBC driver.");
            e.printStackTrace();
           // return;  
        } 
            
            try
        {   // Connect to the database or create if it don't exist 
            con = DriverManager.getConnection(DBH); 
            System.out.println("* Creating HSQLDB connection...");
        }
        catch(Exception e)
        {
            System.err.println("Error: " + e.getMessage());
            //return;
        }
            
            try
        {
            st  = con.createStatement(); 
            System.out.println("* Creating HSQLDB statement...");
        }
        catch (Exception e)
        {  
            System.err.println("Error: createStatement: " + e.getMessage());   
            //return;
        }
            
           // ArrayList <MemoriaForm> lista=null;
            
            MemoriaForm tf = (MemoriaForm)form;
         

               //session.setAttribute("nombre", nombre);
            //lista=gt.BuscarTalleresUsuario(tf);
            sql="SELECT * FROM clientes order by idcliente asc";
           
            try{
                ArrayList <MemoriaForm> lista=null;
                System.out.println("empezando a leer datos bd");
                res=st.executeQuery(sql);
                //st.executeUpdate("INSERT INTO student VALUES ('3', 'Ricardo', 'Garrido', 'Sol, 12', 'Sevilla', '1234', 'r.garrido@gmail.com', '609555321', '0')");
                System.out.println("Leidos datos bd");
                while(res.next()){
                    tf.setNombre(res.getString("nombre"));
                    tf.setContrasena(res.getString("telefono"));
                    System.out.println("dentro while");
                    //lista.add(tf);
                    
            }
                
            }
            catch (SQLException e){
                System.out.println("error sql");
                 e.printStackTrace();
            }
           // tf.setNombre("riki");
            
            request.setAttribute("Mostrardatos", tf);
            
 
            
            System.out.println("he llegadoohasta aqui");
        return mapping.findForward("success");
        }        
}

